package mornd.test4;


import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.ResourcePropertySource;

import java.lang.reflect.Constructor;

/**
 * @author: mornd
 * @dateTime: 2023/5/10 - 19:28
 * boot 的 environment 对象
 */
public class App{
    public static void main(String[] args) throws Exception {
        Class<?> clazz = Class.forName("org.springframework.boot.ApplicationEnvironment");
        Constructor<?> constructor = clazz.getDeclaredConstructor();
        constructor.setAccessible(true);
        StandardEnvironment environment = (StandardEnvironment) constructor.newInstance();

        // 手动创建容器方式不能直接获取 application.properties 下的值，需添加源
        environment.getPropertySources().addLast(new ResourcePropertySource(new ClassPathResource("application.properties")));
        MutablePropertySources propertySources = environment.getPropertySources();
        for (PropertySource<?> ps : propertySources) {
            System.out.println(ps);
        }
        // jvm 启动修改参数： -DJava_Home=abc
        System.out.println(environment.getProperty("Java_Home"));
        // app.properties 添加自定义
        System.out.println(environment.getProperty("mornd.age"));
    }
}
